<?php
declare (strict_types=1);

namespace app\index\controller;

use app\admin\controller\Upload;
use think\facade\Log;

class Uploads extends Base
{
    /**
     * 图片上传
     * @return array|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function imageUploads()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:'.$userInfo['nickname'].'正在上传图片，数据:'.json_encode($_FILES),'error');

        if (!empty($_FILES['file'])) {
            Log::write('请求图片上传','error');
            return Upload::uploadImages(request()->param('path'));
        }
    }

    /**
     * 视频上传
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function videoUploads()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '正在上传视频，数据：'.json_encode($_FILES), 'error');

        if (!empty($_FILES['file'])) {
            return Upload::uploadVideo($_FILES['file'], request()->param('path'));
        }
    }

    /**
     * 图片/视频删除
     */
    public function delImageOrVideo()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '正在删除图片/视频', 'error');

        if (request()->isPost()) {
            $path = request()->param('path');
            if ($path) {
                return Upload::delImageOrVideo($path);
            }
        }
    }
}